package cj.web.admin.controllers;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.Constants;
import cj.utils.ReturnUUID;
import cj.web.admin.dao.NewsstandInfoDAO;
import cj.web.admin.domain.NewsstandInfo;
import cj.web.login.controllers.BaseController;

@Path("/newsstandInfo")
public class NewsstandInfoController extends BaseController{

	private static Logger logger = Logger.getLogger(NewsstandInfoController.class.getName());
	
	@Autowired
	private NewsstandInfoDAO newsstandInfoDao;

	@Get("show")
	public String show(Model model,NewsstandInfo newsstandInfo)
	{
		return "/views/portal/newsstandInfoList.vm";
	}

	@Post("getList")
	public String getUserList(NewsstandInfo newsstandInfo) {
		
		JSONObject jo = new JSONObject();
		
		List<NewsstandInfo> objs = newsstandInfoDao.query(newsstandInfo.getAddress(), newsstandInfo.getStart(), newsstandInfo.getLength());
		
		int size = newsstandInfoDao.querysize(newsstandInfo.getAddress());
		jo.put("data", objs);
		jo.put("draw", newsstandInfo.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		return "@"+jo.toString();
	}
	
	@Get("queryById")
	public String queryById(Model model,@Param("id") String id)
	{
		newsstandInfoDao.queryById(id);
		return null;
	}
	
	@Get("createPage")
	public String createPage()
	{
		return "/views/portal/newsstandInfoDetail.vm";
	}
	
	@Post("create")
	public String create(Model model,NewsstandInfo newsstandInfo,HttpServletRequest request)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			newsstandInfo.setId(ReturnUUID.getUUID());
			newsstandInfo.setCreateUser(returnSessionObj(request).getAccount());
			newsstandInfo.setDeleteFlag(Constants.deleteFlag.deleteFlag_1);
			newsstandInfo.setUpdateUser(returnSessionObj(request).getAccount());
			newsstandInfoDao.create(newsstandInfo);
		} catch (Exception e) {
			// TODO: handle exception
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("edit")
	public String edit(Model model,@Param("id") String id)
	{
		NewsstandInfo obj = newsstandInfoDao.queryById(id);
		model.add("obj", obj);
		return "/views/portal/newsstandInfoEdit.vm";
	}
	
	@Post("update")
	public String update(Model model,NewsstandInfo newsstandInfo,HttpServletRequest request)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			newsstandInfo.setUpdateUser(returnSessionObj(request).getAccount());
			newsstandInfoDao.update(newsstandInfo);
		} catch (Exception e) {
			// TODO: handle exception
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("deleteById")
	public String deleteById(Model model,@Param("id") String id)
	{
		newsstandInfoDao.deleteById(id);
		return "r:show";
	}
}
